const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const port = process.env.PORT || 5000
// import the plugin of passport to vertify the token 
const passport = require('passport')

// import user.js
const users = require('./routers/api/users')
// import profile.js
const profiles = require('./routers/api/profiles')

// 使用body-parse中间件
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())

// 初始化passport
app.use(passport.initialize())
// passport 配置
require('./config/passport')(passport)


// connect mongodb
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/management-admin', {
  useFindAndModify: true,
  useNewUrlParser: true,
  useCreateIndex: true
}).then(() => {
  console.log('数据库连接成功')
}).catch(err => {
  console.log(err)
})


// use routes
app.use('/api/users', users)
app.use('/api/profiles', profiles)


// listen port 
app.listen(port, (err) => {
  if (!err) {
    console.log(`server is running at localhost:${port}`)
  } else {
    console.log(err)
  }
})